﻿namespace OutLook.Model.Domains
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using OutLook.Model.Infrastructure;

    /// <summary>
    /// Row of Charge included to Load
    /// </summary>
    [DataContract]
    public class ChargeRowToLoad
    {
        #region Constructors

        /// <summary>
        /// Initializes new default instance of ChargeRowToLoad
        /// </summary>
        private ChargeRowToLoad() : this(Charge.Create(), 0.0d) { }

        /// <summary>
        /// Initializes new instance of ChargeRowToLoad
        /// </summary>
        /// <param name="charge">TODO:Nastya</param>
        /// <param name="weight">TODO:Nastya</param>
        private ChargeRowToLoad(Charge charge, double weight)
        {
            ChargeRow = charge;
            Weight = weight;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Charge
        /// </summary>
        [DataMember(Order = 1)]
        public Charge ChargeRow { get; set; }

        /// <summary>
        /// Weight of Charge
        /// </summary>
        [DataMember(Order = 2)]
        public double Weight { get; set; }

        #endregion

        #region Factory Method

        /// <summary>
        /// Creates new default instance of ChargeRowToLoad
        /// </summary>
        /// <returns>New default instance of ChargeRowToLoad</returns>
        public static ChargeRowToLoad Create()
        {
            return new ChargeRowToLoad();
        }

        /// <summary>
        /// Creates new instance of ChargeRowToLoad
        /// </summary>
        /// <param name="charge">TODO:Nastya</param>
        /// <param name="weight">TODO:Nastya</param>
        /// <returns>New instance of ChargeRowToLoad</returns>
        /// <remarks>Can return NULL if input arguments were wrong</remarks>
        public static ChargeRowToLoad Create(Charge charge, double weight)
        {
            if (charge == null)
                return null;

            return new ChargeRowToLoad(charge, weight);
        }

        #endregion

        #region Methods

        /// <summary>
        /// TODO:NASTYA
        /// </summary>
        public void WeightCalculation()
        {
            foreach (ChemicalСombination comp in ChargeRow.Compositions)
            {
                double tempCompWeight = Weight * (1 - (ChargeRow.Wet / 100) - (ChargeRow.Volatiles / 100)) * (comp.Percentage / 100);
                foreach (string item in Constants.COKE)
                {
                    if ((comp.Element.Equals(Constants.ELEMENT_S)) & ((ChargeRow.Title.Contains(item)) & !(ChargeRow.Title[0].Equals(Constants.CHARGE_START_A))))
                    {
                        tempCompWeight *= 0.95;
                    }
                }

                comp.Weight = Math.Round(tempCompWeight, 3);
            }
        }

        #endregion

        #region Equals Charge Weight

        /// <summary>
        /// TODO:NASTYA
        /// </summary>
        /// <param name="other">TODO:NASTYA</param>
        /// <returns>TODO:NASTYA</returns>
        public bool EqualsChargeWeight(List<ChemicalСombination> other)
        {
            bool result = true;
            int EqualElements = 0;
            foreach (ChemicalСombination comp in ChargeRow.Compositions)
            {
                foreach (ChemicalСombination othercomp in other)
                {
                    if (comp.Element == othercomp.Element)
                    {
                        EqualElements++;
                        if (comp.Weight != othercomp.Weight)
                        {
                            result = false;
                            break;
                        }
                    }
                }
            }
            if (EqualElements != ChargeRow.Compositions.Count)
            {
                result = false;
            }
            return result;
        }

        #endregion
    }
}
